<?php
/**
 * 大商创发货接口相关信息
 * auther sk
 * date 20201226
 */

namespace App\Http\Controllers\Dsc\Model;


use App\Http\model\Poll;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class Send extends Model
{
//    protected $table = 'region';
    protected $upd_res = 2;


    /**
     * 吉客云发货接口
     * auther sk
     * date 20201230
     * @param $request
     * @return int
     */
    public static function syncChangesModel($request)
    {
//        dd($request['LogisticName']);
        Log::info('===发货类型===' . $request['SendType']);
        Log::info('===订单号===' . $request['PlatOrderNo']);
        Log::info('===快递单号===' . $request['LogisticNo']);
        Log::info('===快递名称===' . $request['LogisticName']);
        //正常快递发货
        if ($request['SendType'] == 'JH_01') {
            $res_data = mysql_cont('order_info')
                ->where(['order_sn' => $request['PlatOrderNo'],
                ])
                ->whereIn('shipping_status', [1, 2])
                ->exists();
//            dd($res_data);
            if ($res_data == true) {
                return 1;
            } else {
                $pollwhere[] = ['kname', $request['LogisticName']];
                $pollinfo = Orderinfo::get_info('pollinfo', $pollwhere, '*');
                $order_where = ['order_sn' => $request['PlatOrderNo']];
                $order_data = Orderinfo::get_info('order_info', $order_where, '*');
                $delivery_sn = get_delivery_sn();
//                dd($delivery_sn);
                $insert_data = ['delivery_sn' => $delivery_sn,
                    'order_sn' => $order_data->order_sn,
                    'order_id' => $order_data->order_id,
                    'invoice_no' => $request['LogisticNo'],
                    'express_code'=> $request['LogisticNo'],
                    'add_time' => time(),
                    'shipping_id' => $pollinfo->shipping_id,
                    'shipping_name' => $pollinfo->shipping_name,
                    'user_id' => $order_data->user_id,
                    'action_user' => 'admin',
                    'consignee' => $order_data->consignee,
                    'address' => $order_data->address,
                    'country' => $order_data->country,
                    'province' => $order_data->province,
                    'city' => $order_data->city,
                    'district' => $order_data->district,
                    'zipcode' => $order_data->zipcode,
                    'street' => 0,
                    'mobile' => $order_data->mobile,
                    'update_time' => time(),
                    'suppliers_id' => 0,
//                    'status' => 0,

                ];
//                dd($pollinfo->shipping_name);
                $res = mysql_cont('delivery_order')
                    ->where(['order_sn' => $request['PlatOrderNo'],
                    ])
                    ->exists();
                if ($res==false){
                    Orderinfo:: check_insert('delivery_order', $insert_data);
                }
                $update_data =
                    [
                        'invoice_no' => $request['LogisticNo'],
                        'express_code'=> $request['LogisticNo'],
                        'shipping_name' => $pollinfo->shipping_name,
                        'shipping_code' => $pollinfo->shipping_code,
                        'shipping_time' => time(),
                        'shipping_status' => 1,
                        'shipping_id' => $pollinfo->shipping_id,
                    ];
                $where = ['order_sn' => $request['PlatOrderNo']];
                $upd_res = Orderinfo::update_order($where, $update_data);//修改发货
               // Log::info('-----' . $upd_res);
            }

        }
        if ($request['SendType'] == 'JH_02') {
            Log::info('===发货类型JH_02===' . $request['SendType']);
        }
        //无需物流
        if ($request['SendType'] == 'JH_03') {
            $res_data = mysql_cont('order_info')
                ->where(['order_sn' => $request['PlatOrderNo'],
                ])
                ->whereIn('shipping_status', [1, 2])
                ->exists();
            $log=mysql_cont('order_info')
                ->where(['order_sn' => $request['PlatOrderNo'],
                ])
                ->whereIn('shipping_status', [1, 2])->first(['shipping_status','order_sn']);


            Log::info('===$log===' . $log);
            if ($res_data == true) {
                return 1;
            }else{
                $update_data =
                    [
                        'invoice_no' => '线下自提订单',
                        'shipping_name' => '线下配送',
                        'shipping_status' => 1
                    ];
                $where = ['order_sn' => $request['PlatOrderNo']];
                $upd_res = Orderinfo::update_order($where, $update_data);//JH_03修改订单状态
            }
        }
        if ($request['SendType'] == 'JH_04') {
            Log::info('===发货类型JH_04===' . $request['SendType']);
        }
        return $upd_res;


    }

}